#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char* argv[]) {
    int fd;
    char buff[64] = { 0 };
    int ret;

    if(argc < 2) {
        printf("usage: %s <char device path>\r\n", argv[0]);
        printf("   eg: %s /dev/mymisc\r\n", argv[0]);
        return -1;
    }

    fd = open(argv[1], O_RDWR);
    if(fd < 0) {
        perror("open error\n");  // 在应用中打印
        return fd;
    }

    ret = lseek(fd, 0x10000, SEEK_SET);
    printf("lseek ret = %d\r\n", ret);

    ret = read(fd, buff, sizeof(buff));
    printf("read ret = %d, buff = '%s'\r\n", ret, buff);

    const char *msg = "this is from userspace.";
    ret = write(fd, msg, strlen(msg));
    printf("read ret = %d\r\n", ret);

    ret = ioctl(fd, 12, 34);
    printf("ioctl ret = %d\r\n", ret);

    struct pollfd pfd = {.fd = fd, .events = 0, .revents = 0};
    ret = poll(&pfd, 1, 3000);
    printf("poll ret = %d, pfd.revents = %d\r\n", ret, pfd.revents);

    close(fd);
    return 0;
}
